/*
 * DragGroup.fx
 *
 * Copyright (c) 2010 Remi Barraquand <dev at barraquand.com>. All rights reserved.
 *
 * This file is part of FxTools
 *
 * FxTools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FxTools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FxTools. If not, see <http://www.gnu.org/licenses/>.
 */

package com.barraquand.labs.jfxtools.scene;

import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.MouseButton;

/**
 * DragGroup
 *
 * When using CtrlDown, the node stop being dragged when releasing Ctrl but as
 * soon as you press Ctrl back (without releasing the mouse) the node take the
 * final position.
 *
 * @author Remi Barraquand <dev at barraquand.com>
 */

public class DragGroup extends Group {

    var startDragX = 0.0;
    var startDragY = 0.0;

    public var dragButton: MouseButton = MouseButton.PRIMARY;
    public var dragOnCtrlDown: Boolean = Boolean.TRUE;

    override def onMousePressed = function(e:MouseEvent):Void {
        var goHead = if (dragOnCtrlDown) then e.controlDown else Boolean.TRUE;
        if( e.button == dragButton and goHead ) {
            startDragX = e.dragX-translateX;
            startDragY = e.dragX-translateY;
        }
    }

    override def onMouseDragged = function(e:MouseEvent):Void {
        var goHead = if (dragOnCtrlDown) then e.controlDown else Boolean.TRUE;
        if( ((MouseButton.PRIMARY == dragButton and e.primaryButtonDown)
        or  (MouseButton.SECONDARY == dragButton and e.secondaryButtonDown)
        or  (MouseButton.MIDDLE == dragButton and e.middleButtonDown))
        and goHead ) {
            translateX = e.dragX-startDragX;
            translateY = e.dragY-startDragY;
        }
    }
}
